package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.entity.system.AccountLinkLabel;
import com.info.zhiduoduo.repository.mapper.system.AccountLinkLabelMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tianyonghong
 * @create 2023/10/18
 */
@Service
public class AccountLinkLabelService extends BaseService {

    @Resource
    AccountLinkLabelMapper accountLinkLabelMapper;

    public SmartResult<AccountLinkLabel> queryList(BaseSimpleFormBean<AccountLinkLabel> formBean){
        AccountLinkLabel reqdata = formBean.getReqdata();

        Page<AccountLinkLabel> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        accountLinkLabelMapper.queryList(reqdata.getType(),reqdata.getLabel());
        return SmartResult.ofPage(page);
    }

    public SmartResult saveOrUpdate(BaseSimpleFormBean<AccountLinkLabel> formBean) {
        AccountLinkLabel accountLinkLabel = formBean.getReqdata();
        accountLinkLabel.setUpdateTime(new Date());
        if (accountLinkLabel.getId() == null || accountLinkLabel.getId() == 0) {
            accountLinkLabel.setCreateTime(new Date());
            accountLinkLabel.insert();
        } else {
            accountLinkLabel.setCreateTime(null);
            boolean result = accountLinkLabel.updateById();
            if (!result) {
                return SmartResult.fail();
            }
        }

        return SmartResult.success();
    }

    public SmartResult deleteById(Integer id) {

        AccountLinkLabel accountLinkLabel = new AccountLinkLabel();
        accountLinkLabel.setId(id);
        boolean result = accountLinkLabel.deleteById();

        if (!result) {
            return SmartResult.fail();
        }
        return SmartResult.success();
    }

    public SmartResult<AccountLinkLabel> queryAllList(String types) {
        List<AccountLinkLabel> list = accountLinkLabelMapper.selectList(Condition.create().in(StringUtils.isNotBlank(types), "type", types));
        return SmartResult.success(list);
    }
}
